//
//  HAPredationVC.m
//  HA
//
//  Created by sy on 2023/12/17.
//

#import "HAPredationVC.h"
#import "HAPredationSubAVC.h"
#import "SGPagingView.h"

@interface HAPredationVC ()<SGPageTitleViewDelegate,SGPageContentViewDelegate>

@property (nonatomic, strong) SGPageTitleView *pageTitleView;

@property (nonatomic, strong) SGPageContentView *pageContentView;

/***/
@property (nonatomic, strong) UIView *topView;
/***/
@property (nonatomic, strong) UIImageView *s1ImgV;
/***/
@property (nonatomic, strong) UILabel *s1NL;
/***/
@property (nonatomic, strong) UIImageView *s2ImgV;
/***/
@property (nonatomic, strong) UILabel *s2NL;
/***/
@property (nonatomic, strong) UIImageView *s3ImgV;
/***/
@property (nonatomic, strong) UILabel *s3NL;


@end

@implementation HAPredationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.baseTLabel.text = @"分润明细";
    
    NSMutableArray *titArr = [NSMutableArray array];
    NSMutableArray *vcArr = [NSMutableArray array];
    
    [titArr addObject:@"全部"];
    [titArr addObject:@"分润"];
    [titArr addObject:@"返现"];

    HAPredationSubAVC *vc0 = [[HAPredationSubAVC alloc]init];
    vc0.source_id = @"0";
    HAPredationSubAVC *vc1 = [[HAPredationSubAVC alloc]init];
    vc1.source_id = @"1";
    HAPredationSubAVC *vc2 = [[HAPredationSubAVC alloc]init];
    vc2.source_id = @"2";

    [vcArr addObject:vc0];
    [vcArr addObject:vc1];
    [vcArr addObject:vc2];
    
    self.pageTitleView = [SGPageTitleView pageTitleViewWithFrame:CGRectMake(0, app_topH+44, kUIScreen_w, 44) delegate:self titleNames:titArr titleFont:[UIFont systemFontOfSize:18]];
    [self.view addSubview:self.pageTitleView];
    self.pageTitleView.isTitleGradientEffect = NO;
    self.pageTitleView.indicatorLengthStyle = SGIndicatorLengthStyleEqual;
    self.pageTitleView.isNeedBounces = NO;
    self.pageTitleView.titleColorStateNormal = [UIColor colorWithHexString:@"#666F83"];
    self.pageTitleView.titleColorStateSelected = [UIColor colorWithHexString:@"#21252A"];
    self.pageTitleView.indicatorColor = [UIColor colorWithHexString:@"#000000"];
    self.pageTitleView.indicatorHeight = 1;
    self.pageTitleView.titleTextScaling = 0;
    self.pageTitleView.isOpenTitleTextZoom = YES;
    
    UIView *topView = [UIView ha_createView];
    self.topView = topView;
    [self.view addSubview:topView];
    topView.frame = CGRectMake(0, app_topH+44+44+5, kUIScreen_w, 64);

    CGFloat contentViewHeight = kUIScreen_h-app_topH-64-64-10-app_bottomH;
    self.pageContentView = [[SGPageContentView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(topView.frame)+5, kUIScreen_w, contentViewHeight) parentVC:self childVCs:vcArr];
    self.pageContentView.isScrollEnabled = YES;
    self.pageContentView.delegatePageContentView = self;
    [self.view addSubview:_pageContentView];

    [self.pageContentView setPageCententViewCurrentIndex:0];
    self.pageTitleView.selectedIndex = 0;

    [self.s1ImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(0);
        make.height.mas_offset(64);
        make.width.mas_offset((kUIScreen_w-60)/3);
    }];
    
    [self.s2ImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.height.mas_offset(64);
        make.width.mas_offset((kUIScreen_w-60)/3);
    }];

    [self.s3ImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(0);
        make.height.mas_offset(64);
        make.width.mas_offset((kUIScreen_w-60)/3);
    }];


    [self loadData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handNum:) name:@"KYHpredationNumKey" object:nil];

    // Do any additional setup after loading the view.
}

-(void)handNum: (NSNotification*)noti {
    
    NSArray *dataArr = [noti object];
    
    if (dataArr.count > 0) {
        NSDictionary *dic = dataArr[0];
        if ([[dic allKeys]containsObject:@"number"]) {
            self.s1NL.text = dic[@"number"];
        }
    }
    if (dataArr.count > 1) {
        NSDictionary *dic = dataArr[1];
        if ([[dic allKeys]containsObject:@"number"]) {
            self.s2NL.text = dic[@"number"];
        }
    }
    if (dataArr.count > 2) {
        NSDictionary *dic = dataArr[2];
        if ([[dic allKeys]containsObject:@"number"]) {
            self.s3NL.text = dic[@"number"];
        }
    }
}


- (void)pageTitleView:(SGPageTitleView *)pageTitleView selectedIndex:(NSInteger)selectedIndex {
    [self.pageContentView setPageCententViewCurrentIndex:selectedIndex];
}

- (void)pageContentView:(SGPageContentView *)pageContentView progress:(CGFloat)progress originalIndex:(NSInteger)originalIndex targetIndex:(NSInteger)targetIndex {
    self.pageTitleView.resetSelectedIndex = targetIndex;
}

-(void)loadData {
    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodGET;
    request.requestUrl = @"share/index";
    [MBProgressHUD showLoading:self.view];
    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {
            
            [MBProgressHUD hideHUDForView:self.view];
            
            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                if ([[dataModel.data allKeys]containsObject:@"number1"]) {
                    NSString *number1 = [NSString stringWithFormat:@"%@",[dataModel.data valueForKey:@"number1"]];
                    self.s1NL.text = number1;
                }
                if ([[dataModel.data allKeys]containsObject:@"number2"]) {
                    NSString *number2 = [NSString stringWithFormat:@"%@",[dataModel.data valueForKey:@"number2"]];
                    self.s2NL.text = number2;
                }
                if ([[dataModel.data allKeys]containsObject:@"number3"]) {
                    NSString *number3 = [NSString stringWithFormat:@"%@",[dataModel.data valueForKey:@"number3"]];
                    self.s3NL.text = number3;
                }

            }else{
                [MBProgressHUD showError:dataModel.msg];
            }
        }
    }];
}


- (UIImageView *)s1ImgV
{
    if (!_s1ImgV) {
        _s1ImgV = [UIImageView ha_createImageView];
        CGFloat imgW = (kUIScreen_w-60)/3;
        _s1ImgV.image = [UIImage gradientImageWithBounds:CGRectMake(0, 0, imgW, 64) andColors:@[[UIColor colorWithHexString:@"#4876F1"],[UIColor colorWithHexString:@"#91ABF4"]] andGradientType:0];
        [self.topView addSubview:_s1ImgV];
        
        self.s1NL = [UILabel ha_NLabelWithFont:15 textColor:@"#ffffff" text:@"0.00" alignment:1];
        [_s1ImgV addSubview:self.s1NL];
        [self.s1NL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(-8);
            make.height.mas_offset(14);
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
        }];
        
        UILabel *tL = [UILabel ha_NLabelWithFont:12 textColor:@"#ffffff" text:@"累计收益" alignment:1];
        [_s1ImgV addSubview:tL];
        [tL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(12);
            make.height.mas_offset(14);
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
        }];
    }
    return _s1ImgV;
}

- (UIImageView *)s2ImgV
{
    if (!_s2ImgV) {
        _s2ImgV = [UIImageView ha_createImageView];
        CGFloat imgW = (kUIScreen_w-60)/3;
        _s2ImgV.image = [UIImage gradientImageWithBounds:CGRectMake(0, 0, imgW, 64) andColors:@[[UIColor colorWithHexString:@"#6AE176"],[UIColor colorWithHexString:@"#93F6AF"]] andGradientType:0];
        [self.topView addSubview:_s2ImgV];

        self.s2NL = [UILabel ha_NLabelWithFont:15 textColor:@"#ffffff" text:@"0.00" alignment:1];
        [_s2ImgV addSubview:self.s2NL];
        [self.s2NL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(-8);
            make.height.mas_offset(14);
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
        }];

        UILabel *tL = [UILabel ha_NLabelWithFont:12 textColor:@"#ffffff" text:@"累计分润" alignment:1];
        [_s2ImgV addSubview:tL];
        [tL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(12);
            make.height.mas_offset(14);
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
        }];
    }
    return _s2ImgV;
}


- (UIImageView *)s3ImgV
{
    if (!_s3ImgV) {
        _s3ImgV = [UIImageView ha_createImageView];
        CGFloat imgW = (kUIScreen_w-60)/3;
        _s3ImgV.image = [UIImage gradientImageWithBounds:CGRectMake(0, 0, imgW, 64) andColors:@[[UIColor colorWithHexString:@"#E95D5E"],[UIColor colorWithHexString:@"#EF9D9F"]] andGradientType:0];
        [self.topView addSubview:_s3ImgV];

        self.s3NL = [UILabel ha_NLabelWithFont:14 textColor:@"#ffffff" text:@"0.00" alignment:1];
        [_s3ImgV addSubview:self.s3NL];
        [self.s3NL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(-8);
            make.height.mas_offset(14);
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
        }];

        UILabel *tL = [UILabel ha_NLabelWithFont:12 textColor:@"#ffffff" text:@"累计返现" alignment:1];
        [_s3ImgV addSubview:tL];
        [tL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(12);
            make.height.mas_offset(14);
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
        }];

    }
    return _s3ImgV;
}


@end
